
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#include "RTPrimBox.h"

namespace RT
{
	/*Constructor*/
	RTPrimBox::RTPrimBox(F32 width, F32 height, F32 depth):RTIPrimitive(),m_width(width), m_height(height), 
				m_depth(depth), m_idHitFace(0)
	{
		m_vertex[0] = RTVector3f(-width/2, -height/2, -depth/2);
		m_vertex[1] = RTVector3f(-width/2, height/2, -depth/2);
		m_vertex[2] = RTVector3f(width/2, height/2, -depth/2);
		m_vertex[3] = RTVector3f(width/2, height/2, depth/2);

		m_vertex[4] = RTVector3f(width/2, -height/2, depth/2);
		m_vertex[5] = RTVector3f(-width/2, -height/2, depth/2);
		m_vertex[6] = RTVector3f(-width/2, -height/2, -depth/2);
		m_vertex[7] = RTVector3f(-width/2, height/2, -depth/2);
	}

	/*Destructor*/
	RTPrimBox::~RTPrimBox(){}

	/*Check intersection between this primitive and a ray*/
	bool RTPrimBox::intersect(const RTRay& ray, F32& dist, bool useGlobalCoordinates)
	{
		//change the coordinate system
		RTRay modRay;
		if (useGlobalCoordinates)
			globalToLocal(ray, modRay);
		else
			convertRayToLocalCoordinates(ray, modRay);

		//initialiase data
		dist = -1;
		m_idHitFace = 0;

		//for very face
		for(I32 i = 0; i < 6; i++)
		{
			//check intersection with a face
			F32 t = -1;
			bool res = intersectRayWithFace(modRay, m_vertex[i+1], m_vertex[i], m_vertex[i+2], t);

			//if no I32eresection, continue
			if(!res)
				continue;

			if(dist == -1 && t > 0)//first face, set automatically dist
			{
				m_idHitFace = i;
				dist = t;
			}
			else if(t < dist && t > 0)
			{
				m_idHitFace = i;
				dist = t;
			}
		}

		if(dist <= 0)
			return false;
	
		return true;
	}

	/*Calculate the normal of the primitive in a given point*/
	void RTPrimBox::computeNormal(const RTRay& ray, F32 /*dist*/, RTVector3f& normal)const
	{
		//get the normal
		switch(m_idHitFace)
		{
		case 0://front
			normal = RTVector3f(0, 0, -1);
			break;
		case 1://top
			normal = RTVector3f(0, 1, 0);
			break;
		case 2://right
			normal = RTVector3f(1, 0, 0);
			break;
		case 3://back
			normal = RTVector3f(0, 0, 1);
			break;
		case 4://bottom
			normal = RTVector3f(0, -1, 0);
			break;
		case 5://left
			normal = RTVector3f(-1, 0, 0);
			break;
		}

		//change the coordinate system
		RTRay r;
		convertRayToLocalCoordinates(ray, r);

		//if we are inside an object
		if(r.getDirection().dot(normal) > 0)
			normal = normal * -1;

		//apply the wm to the normal
		convertNormalToGlobalCoordinates(normal, normal);

	
	}

	/*Check if the point is inside the primitive*/
	bool RTPrimBox::isInside(const RTVector3f& p)const
	{
		//convert p to local coordinates
		RTVector3f localP;
		convertPointToLocalCoordinates(p, localP);

		F64 x = m_width / 2;
		F64 y = m_height / 2;
		F64 z = m_depth / 2;
		if(localP.x >= -x && localP.x <= x && localP.y >= -y && localP.y <= y && localP.z >= -z && localP.z <= z)
			return true;

		return false;

	}

	/*Check if a ray intersect with a face of the box*/
	bool RTPrimBox::intersectRayWithFace(const RTRay& r, const RTVector3f& p0, const RTVector3f& p1, 
			const RTVector3f& p2, F32& dist)const
	{
		//get origin and direction of the ray
		RTVector3f d = r.getDirection();
		RTVector3f o = r.getOrigin();


		RTVector3f w(o.x - p0.x, o.y - p0.y, o.z - p0.z);

		RTVector3f u = p1 - p0;
		RTVector3f v = p2 - p0;

		F32 D = -(u.cross(v)).dot(d);
		F32 a = -(w.cross(v)).dot(d) / D;
		F32 b = -(u.cross(w)).dot(d) / D;

		dist = (u.cross(v)).dot(w) / D;

		if(dist <= 0)
			return false;

		if(a > 0 && b > 0 && a<= 1 && b <= 1)
			return true;

		return false;
	}

}